package com.ybbase.framework.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.ybbase.framework.mapper.system.PermissionMapper;
import com.ybbase.framework.mapper.system.RolePermissionMapper;
import com.ybbase.framework.mapper.system.UserRoleMapper;
import com.ybbase.framework.model.po.system.Permission;
import com.ybbase.framework.model.po.system.Role;
import com.ybbase.framework.mapper.system.RoleMapper;
import com.ybbase.framework.model.po.system.RolePermission;
import com.ybbase.framework.model.po.system.UserRole;
import com.ybbase.framework.service.system.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 系统角色 服务实现类
 * </p>
 *
 * @author cg
 * @since 2019-11-25
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    RoleMapper roleMapper;
    @Autowired
    PermissionMapper permissionMapper;
    @Autowired
    RolePermissionMapper rolePermissionMapper;
    @Autowired
    UserRoleMapper userRoleMapper;

    /**
     * 根据用户编码获取角色列表
     *
     * @param userId 用户编码
     * @return List<Role>
     * @author cg
     * @date 2019-11-26 17:41
     * @since cloud2.0
     */
    @Override
    public List<Role> getRoleListByUserId(Integer userId) {
        return roleMapper.getRoleListByUserId(userId);
    }


    /**
     * 保存角色
     *
     * @param role 角色编码
     * @author cg
     * @date 2019-11-29 17:26
     * @since cloud2.0
     */
    @Override
    public void saveRole(Role role) {
        if (role.getId() != null) {
            roleMapper.updateById(role);
            QueryWrapper<RolePermission> rolePermissionQueryWrapper = new QueryWrapper<>();
            rolePermissionQueryWrapper.lambda().eq(RolePermission::getRoleId, role.getId());
            rolePermissionMapper.delete(rolePermissionQueryWrapper);
        } else {
            roleMapper.insert(role);
        }
        if (role.getPermissionId() != null && role.getPermissionId().size() > 0) {
            for (String permissionId : role.getPermissionId()) {
                RolePermission rolePermission = new RolePermission();
                rolePermission.setPerId(Integer.parseInt(permissionId));
                rolePermission.setRoleId(role.getId());
                rolePermissionMapper.insert(rolePermission);
            }

        }
    }

    /**
     * 删除角色
     *
     * @param roleId 角色编码
     * @author cg
     * @date 2019-11-29 17:29
     * @since cloud2.0
     */
    @Override
    public void removeRole(Integer roleId) {
        roleMapper.deleteById(roleId);
    }

    @Override
    public void removeRolePer(Integer roleId) {
        roleMapper.delRolePer(roleId);
    }

    @Override
    public List<UserRole> findRoidByUser(Integer role_id) {
        return userRoleMapper.findRoidByUser(role_id);
    }

    @Override
    public List<Integer> findUserId(Integer role_id) {
        return userRoleMapper.findUserId(role_id);
    }
}
